package speedchina.controller.work;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import speedchina.controller.BaseController;
import speedchina.dto.PageParamDTO;
import speedchina.entity.user.Dept;
import speedchina.entity.work.Project;
import speedchina.enums.ResultEnum;
import speedchina.exception.BaseException;
import speedchina.service.user.DeptService;
import speedchina.service.work.ProjectService;
import speedchina.vo.LayuiTableVO;
import speedchina.vo.ResultVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 项目控制器
 *
 * @author earthchen
 * @date 2018/9/29
 **/
@Controller
@RequestMapping("/project")
public class ProjectController extends BaseController
{

    @Autowired
    private ProjectService projectService;

    @Autowired
    private DeptService deptService;

    /**
     * 分页获取项目页面
     *
     * @param model 模型
     * @return 项目页面
     */
    @RequiresPermissions("system:project:view")
    @GetMapping("/view")
    public String project(Model model)
    {
        List<Dept> deptList = deptService.list(
            new QueryWrapper<Dept>().eq("status", "0").eq("del_flag", "0"));
        model.addAttribute("deptList", deptList);
        return "project/task";
    }

    /**
     * 跳转更新页面
     *
     * @param projectId 项目id
     * @param model     模型
     * @return 更新页面
     */
    @GetMapping("/update/{projectId}")
    public String updatePage(@PathVariable("projectId") Long projectId, Model model)
    {
        Project project = projectService.getById(projectId);
//        Dept dept = deptService.selectDeptById(Long.valueOf(project.getDeptId()));
//        model.addAttribute("dept", dept);
        model.addAttribute("project", project);
        return "project/update";
    }

    /**
     * 分页获取项目
     *
     * @param pageParamDTO 分页参数
     * @param projectName  项目名
     * @return 项目的layui table
     */
    @RequiresPermissions("system:project:list")
    @GetMapping("/list")
    @ResponseBody
    public LayuiTableVO list(PageParamDTO pageParamDTO, String projectName, String projectId)
    {
        IPage<Project> page = new Page<>(pageParamDTO.getPage(), pageParamDTO.getSize());
        page = projectService.page(page,
            new QueryWrapper<Project>()
                .like(StringUtils.isNotBlank(projectName),"project_name", projectName)
                .like(StringUtils.isNotBlank(projectId),"project_id", projectId)
                .orderByDesc("update_time")
        );
        return getTableInfo(page);

    }

    /**
     * 根据id获取当前项目信息
     *
     * @param projectId 项目id
     * @return 统一结果
     */
    @GetMapping("/{projectId}")
    @ResponseBody
    public ResultVO getById(@PathVariable("projectId") Long projectId)
    {
        return ResultVO.success(projectService.getById(projectId));
    }

    /**
     * 增加项目
     *
     * @param project 项目信息
     * @return 统一结果
     */
    @RequiresPermissions("system:project:add")
    @PostMapping("/add")
    @ResponseBody
    public ResultVO add(Project project)
    {
        if (project.getStartDate().getTime() > project.getEndDate().getTime())
        {
            throw new BaseException(ResultEnum.PROJECT_STARTDATE_MUST_LT_ENDDATE);
        }
        project.setCreateBy(getLoginName());
        List<Project> projectList = projectService.list(
            new QueryWrapper<Project>()
                .eq("del_flag",0)
                .eq(StringUtils.isNotBlank(project.getProjectName()),"project_name", project.getProjectName())
                .or()
                .eq(StringUtils.isNotBlank(project.getProjectId()),"project_id", project.getProjectId()));
        if (!projectList.isEmpty())
        {
            throw new BaseException(ResultEnum.PROJECT_NAME_OR_PROJECTID_ALEARLY_HAVE);
        }
        project.setUpdateTime(new Date());
        return ResultVO.success(projectService.save(project));
    }

    /**
     * 修改项目
     *
     * @param projectId 项目id
     * @param project   项目信息
     * @return 统一返回结果
     */
    @RequiresPermissions("system:project:edit")
    @PutMapping("/update/{projectId}")
    @ResponseBody
    public ResultVO update(@PathVariable("projectId") Long projectId, Project project)
    {
        project.setId(projectId);
        project.setUpdateTime(new Date());
        return ResultVO.success(projectService.updateById(project));
    }

    /**
     * 删除指定id的项目
     *
     * @param projectId 项目id
     * @return 统一结果
     */
    @RequiresPermissions("system:project:delete")
    @DeleteMapping("/del/{projectId}")
    @ResponseBody
    public ResultVO del(@PathVariable("projectId") Long projectId)
    {
        return ResultVO.success(projectService.removeById(projectId));
    }

}
